package com.tz.farmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tz.farmer.mapper.FarmerSunDataMapper;
import com.tz.farmer.model.FarmerSunData;
import com.tz.farmer.service.IFarmerSunDataService;
import com.tz.farmer.so.DefaultSo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Map;
import java.util.TreeMap;

@Service
public class FarmerSunDataServiceImpl extends ServiceImpl<FarmerSunDataMapper, FarmerSunData> implements IFarmerSunDataService {

    @Override
    public Map<String, Object> getSunData(String market, String keyA, String keyB, String keyC) {
        Map<String, Object> response = new TreeMap<>();

        FarmerSunData factory = this.getOne(new LambdaQueryWrapper<FarmerSunData>()
            .eq(FarmerSunData::getMarket, market)
            .eq(FarmerSunData::getCode, keyA));

        FarmerSunData enterprise = this.getOne(new LambdaQueryWrapper<FarmerSunData>()
            .eq(FarmerSunData::getMarket, market)
            .eq(FarmerSunData::getCode, keyB));

        FarmerSunData device = this.getOne(new LambdaQueryWrapper<FarmerSunData>()
            .eq(FarmerSunData::getMarket, market)
            .eq(FarmerSunData::getCode, keyC));

        if (factory != null && !ObjectUtils.isEmpty(factory.getValue())) {
            response.put(keyA.replace("Key", ""), factory.getValue());
        }

        if (enterprise != null && !ObjectUtils.isEmpty(enterprise.getValue())) {
            response.put(keyB.replace("Key", ""), enterprise.getValue());
        }

        if (device != null && !ObjectUtils.isEmpty(device.getValue())) {
            response.put(keyC.replace("Key", ""), device.getValue());
        }

        return response;
    }

    @Override
    public void setSunData(DefaultSo body, String market, String keyA, String keyB, String keyC) {
        if (!ObjectUtils.isEmpty(body.getFactory())) {
            this.setData(market, keyA, body.getFactory());
        }

        if (!ObjectUtils.isEmpty(body.getEnterprise())) {
            this.setData(market, keyB, body.getEnterprise());
        }

        if (!ObjectUtils.isEmpty(body.getDevice())) {
            this.setData(market, keyC, body.getDevice());
        }

        if (!ObjectUtils.isEmpty(body.getKitchen())) {
            this.setData(market, keyA, body.getKitchen());
        }

        if (!ObjectUtils.isEmpty(body.getSociety())) {
            this.setData(market, keyB, body.getSociety());
        }

        if (!ObjectUtils.isEmpty(body.getSchool())) {
            this.setData(market, keyC, body.getSchool());
        }
    }

    @Override
    public boolean setData(String market, String dataKey, Double count) {
        FarmerSunData one = getOne(new LambdaQueryWrapper<FarmerSunData>()
            .eq(FarmerSunData::getMarket, market)
            .eq(FarmerSunData::getCode, dataKey));

        if (one == null) {
            one = new FarmerSunData();

            one.setLabel(market);
            one.setMarket(market);
            one.setCode(dataKey);
        }

        one.setValue(count);

        return saveOrUpdate(one);
    }

    @Override
    public Object getData(String market, String dataKey) {
        // 查询一条数据
        FarmerSunData one = this.getOne(new LambdaQueryWrapper<FarmerSunData>()
            .eq(FarmerSunData::getCode, dataKey)
            .eq(FarmerSunData::getMarket, market));

        if (one != null && !ObjectUtils.isEmpty(one.getValue())) {
            return one.getValue();
        }

        return 0;
    }
}
